import { BackTop, Form, Input, Checkbox, Button, Radio, Table, Space, Tag,message } from "antd";
import { useState, useEffect, useRef } from "react";
import { useNavigate } from 'react-router-dom'
import ZingTable from "./ZingTable";
import type { RadioChangeEvent } from 'antd';
import './index.less';
import { getUserLedger,getuser } from '@/services/login'
import { number } from "echarts";
const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
};
const time2time=(time:number)=>{
    let yun = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    let time1=new Date(time)
    let time2=String(time1).split(' ')
    let montn:any=-1
    let time3:any=-1
    yun.forEach((v, i) => {
        if (time2[1] == v) {
            montn = i + 1
        }
    })
    if(montn<10){
        montn="0"+montn
    }
    if(time2[2] && Number(time2[2])<10){
        time3="0"+time2[2]
    }else{
        time3=time2[2]
    }
    return time2[3]+'-'+montn+'-'+time3
}
const Login = () => {
    useEffect(() => {
    }, [])
    const navigate = useNavigate();
    const [messageApi, contextHolder] = message.useMessage()
    const loginModel: any = useRef(null)
    const ledgerModel: any = useRef(null)
    const [typeValue, setTypeValue] = useState('用户');
    const headValue = [
        {name:'公司名',key:'name'},
        {name:'起始日',key:'start'},
        {name:'准则',key:'rule'},
        {name:'操作',key:'operate'}
    ];
    let [bodyValue, setbodyValue] = useState({});
    const chooseLedger = () => {
        ledgerModel.current.style.transform = 'rotateY(180deg)'
        loginModel.current.style.transform = 'rotateY(180deg)'
        setTimeout(() => {
            loginModel.current.style.opacity = '0'
            ledgerModel.current.style.opacity = '1'
            setTimeout(() => {
                loginModel.current.style.display = 'none'
            }, 100);
        }, 400)
    }
    const onFinish = (values: any) => {
        getuser(values).then((res)=>{     
            sessionStorage.setItem('userId',res.data.userId)
            sessionStorage.setItem('token',res.data.token)   
            if (res.code==0) {
                messageApi.open({
                    type: 'success',
                    content: '登陆成功',
                  });
                  setTimeout(()=>{
                    chooseLedger()
                  },600)
                  return getUserLedger()
            }else{
                messageApi.open({
                    type: 'error',
                    content: '查无此人',
                  });
            }       
        }).then((res)=>{
            let _data=res.data.map((v:any)=>{
                return {name:v.companyName,start:time2time(v.openTime),rule:v.accountStandard,operate:['操作']}
            })
            setbodyValue({id:1,content:_data})
            setTimeout(()=>{
            },1000)
        })

    };
    const onChangeType = (e: RadioChangeEvent) => {
        setTypeValue(e.target.value);
    };
    const toSystem=()=>{
        navigate('/home/system')
    }
    return (
        <div className="login">
            {contextHolder}
            <div ref={ledgerModel} className="ledgerModel">
                <div style={{width:400,height:400,marginTop:50,backgroundColor:'rgb(228, 224, 224)',paddingTop:20 }}>
                <div className="ledgerModel1st">
                    <ZingTable theads={headValue} tbodys={bodyValue} operation1={toSystem} Tstyle={{width:380,hbackgroundColor:'rgb(34,171,170)',dlineHeight:5}}></ZingTable>
                </div>
                </div>
            </div>
            <div ref={loginModel} className="loginModel">
                <Form
                    name="basic"
                    labelCol={{ span: 8 }}
                    wrapperCol={{ span: 16 }}
                    style={{ maxWidth: 600 }}
                    initialValues={{ remember: '用户' }}
                    onFinish={onFinish}
                    onFinishFailed={onFinishFailed}
                    autoComplete="off"
                    className="loginForm"
                >
                    <Form.Item
                        label="账户"
                        name="account"
                        rules={[{ required: true, message: '请输入账号名称!' }]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item<FieldType>
                        label="密码"
                        name="password"
                        rules={[{ required: true, message: '请输入账号密码!' }]}
                    >
                        <Input.Password />
                    </Form.Item>
                    {/* <Form.Item<FieldType>
                        name="remember"
                        wrapperCol={{ offset: 8, span: 16 }}
                    >
                        <Radio.Group onChange={onChangeType} >
                            <Radio value={'用户'} >用户</Radio>
                            <Radio value={'管理员'}>管理员</Radio>
                        </Radio.Group>
                    </Form.Item> */}
                    <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                        <Button type="primary" htmlType="submit" >
                            登录
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        </div>
    )
}
export default Login